
***********************************************************************************
* Analysis 3: Who experienced downward mobility among the movers?
* Run discrete choice models for movers
* last modified 5/28/2024
*
*
***********************************************************************************


clear
capture log close 
set more off

***************
* Directories *
***************

cd "/Users/"

global dofile			=	"./Code/NEW-OEWS"
global matrix_data		=   "./Data/National Employment Matrix"
global oews_data		= 	"./Data/OEWS/all_excel"
global onet_data		= 	"./OccLink/ONET/ONET data/merged"
global cps_data			= 	"./Data/CPS/IPUMS/00015"
global xwalk 			=	"./Data/Crosswalk"
global results 			=	"./Results"
global temp				= 	"./Temp"
global logfile          =   "./Results"


log using "${logfile}/TableE5.log", replace
 
***********************************************************************************
* Table 4 logit Model for Upward Mobility Based on 5% Change in Occ Earnings *
***********************************************************************************

use "${temp}/cps_2000-2020_wide_movers.dta", replace

* Define the mobility variable

gen xx = adj_annual_mean_t1/adj_annual_mean_t0

gen mobile = 1 if xx > 1.05 & !missing(adj_annual_mean_t0) & !missing(adj_annual_mean_t1) & occ_t0 != occ_t1 /*upward*/

replace mobile = 2 if inrange(xx, 0.95, 1.05) & !missing(adj_annual_mean_t0) & !missing(adj_annual_mean_t1) & occ_t0 != occ_t1 /*horizontal*/

replace mobile = 0 if xx < 0.95 & !missing(adj_annual_mean_t0) & !missing(adj_annual_mean_t1) & occ_t0 != occ_t1 /*dowanward mobility*/


lab def mobile 0 "downward" 1 "upward" 2 "horizontal" 
lab val mobile mobile

recode mobile (0=1 "downward") (1 2 = 0 "not downward"), gen(downward)
recode mobile (1=1 "upward") (0 2 = 0 "not upward"), gen(upward)

* rescale growth variables in terms of 10 percent change

gen emp_pc_growth10_t0 = emp_pc_growth_t0/10
gen emp_pc_growth10_t1 = emp_pc_growth_t1/10

gen proj_pc_growth10_t0 = proj_pc_growth_t0/10
gen proj_pc_growth10_t1 = proj_pc_growth_t1/10


* Mark the analytical sample

mark good if emp_chng_rate_t0 !=. & emp_chng_rate_t1 != . & /* 
*/ proj_pc_growth_t0 != . & proj_pc_growth_t1 != . & /*
*/ outlook_gr_t0 != .  & outlook_gr_t1 != .  & /*
*/ adj_annual_mean_t0 != . 


* m1: individual variables + 2-year employment change

gen lograte_t0 = log(1+emp_chng_rate_t0)
gen lograte_t1 = log(1+emp_chng_rate_t1)

/* model 1 */
logit upward emp_pc_growth10_t0 emp_pc_growth10_t1               /*
*/ i.male ib2.age_gr ib1.race i.hispanic ib1.edu_gr ib1.year /*
*/ if good == 1  


* m2: individual variables + 10-year projected growth

gen logprojrate_t0 = log(1+proj_pc_growth_t0/100)
gen logprojrate_t1 = log(1+proj_pc_growth_t1/100)

/* model 2 */
logit upward proj_pc_growth10_t0 proj_pc_growth10_t1             /*
*/ i.male ib2.age_gr ib1.race i.hispanic ib1.edu_gr ib1.year /*
*/ if good == 1   

* m3: individual variables + projected OOH outlook categories

/* model 3 */
logit upward ib2.outlook_gr_t0 ib2.outlook_gr_t1            /*
*/	i.male ib2.age_gr ib1.race i.hispanic ib1.edu_gr ib1.year /* 
*/ if good == 1  

/* model 4 */
logit upward ib2.outlook_gr_t0##ib2.outlook_gr_t1           /*
*/	i.male ib2.age_gr ib1.race i.hispanic ib1.edu_gr ib1.year /* 
*/ if good == 1  


log close

*******************************************************************************
* Upward mobility probability CI plot *
*******************************************************************************

/* model 3 */
logit upward ib2.outlook_gr_t0 ib2.outlook_gr_t1            /*
*/	i.male ib2.age_gr ib1.race i.hispanic ib1.edu_gr ib1.year /* 
*/ if good == 1  

/* obtain predicted probability and confidence interval*/
predict phat

predict xb, xb

predict error, stdp

generate lb = xb - invnormal(0.975) * error

generate ub = xb + invnormal(0.975) * error

generate plb = invlogit(lb)

generate pub = invlogit(ub)


/* origin = growing, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 1 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = stable, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = declining, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 3 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* destination = growing, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 1 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* destination = stable, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* destination = declining, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 3 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020


/* model 4 */
logit upward ib2.outlook_gr_t0##ib2.outlook_gr_t1           /*
*/	i.male ib2.age_gr ib1.race i.hispanic ib1.edu_gr ib1.year /* 
*/ if good == 1  

/* obtain predicted probability and confidence interval*/
drop phat xb error lb ub plb pub

predict phat

predict xb, xb

predict error, stdp

generate lb = xb - invnormal(0.975) * error

generate ub = xb + invnormal(0.975) * error

generate plb = invlogit(lb)

generate pub = invlogit(ub)

/* origin = growing, destination = growing, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 1 & outlook_gr_t1 == 1 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = growing, destination = stable, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 1 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = growing, destination = declining, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 1 & outlook_gr_t1 == 3 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = stable, destination = growing, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 1 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = stable, destination = stable, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = stable, destination = declining, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 2 & outlook_gr_t1 == 3 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020


/* origin = declining, destination = growing, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 3 & outlook_gr_t1 == 1 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = declining, destination = stable, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 3 & outlook_gr_t1 == 2 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020

/* origin = declining, destination = declining, all other var at the reference level */
sum phat plb pub if outlook_gr_t0 == 3 & outlook_gr_t1 == 3 & male == 1 & age_gr == 2 & race == 1 & hispanic == 0 & edu_gr == 2 & year == 2020



/* estimate size of workers in growing, stable, and declining occupations */ 
tab outlook_gr_t0 if good == 1 & !missing(upward) 
tab outlook_gr_t1 if good == 1 & !missing(upward)
tab outlook_gr_t0 outlook_gr_t1 if good == 1 & !missing(upward), cell

log close
